<template>
  <div class="dashboard-container">
    <el-card 
      class="box-card"  
      v-loading="loading"
    >
      <!-- 表单输入框 -->
      <el-form 
        ref="form" 
        :model="form" 
        label-width="80px" 
        :inline="true"
      >
        <el-form-item label="学科名称">
          <el-input v-model="form.subjectName"></el-input>
        </el-form-item>
        <!-- 搜索/清除按钮 -->
        <el-button
          class="remove-btn"
          @click="onRemoveAll"
        >
          清除
        </el-button>
        <el-button
          type="primary"
          @click="onSearchSubjectList"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-edit"
          class="addUser"
          @click="onAddSubjectList"
        >
          新增标签
        </el-button>
      </el-form>
      <!-- 新增/修改 -->
      <el-dialog
        :title="subjectForm.subjectName ? '修改学科' : '新增学科'"
        :visible.sync="addVisible"
        width="300"
        v-loading="loading"
      >
        <el-form 
          ref="subjectForm" 
          :model="subjectForm" 
          label-width="80px" 
          :rules="rules"
        >
          <el-form-item 
            label="目录名称" 
            prop="subjectName"
          >
            <el-input 
              v-model="subjectForm.subjectName" 
              placeholder="请输入学科名称"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="是否显示" >
            <el-switch
              v-model="subjectForm.isFrontDisplay"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <span 
          slot="footer" 
          class="dialog-footer"
        >
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button 
            type="primary" 
            @click="onAddSubjectListOk"
          >
            确 定
          </el-button>
        </span>
      </el-dialog>
      <!-- 提示 -->
      <el-alert
        :closable="false"
        :title="`共${subjectList.counts}条纪录`"
        type="info"
        class="hint"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <my-table
        class="container"
        :table-props="{
          data: subjectList.items,
          border: true
        }"
        :columns="[
          { label: '序号', prop: 'index', width: 180, align: 'center' },
          { label: '学科名称', prop: 'subjectName', width: 180, align: 'center'},
          { label: '创建者', prop: 'username', width: 180, align: 'center' },
          { label: '创建日期', prop: 'addDate', width: 180, align: 'center' },
          { label: '前台是否显示', prop: 'isFrontDisplay', width: 180, align: 'center' },
          { label: '二级目录', prop: 'twoLevelDirectory', width: 180, align: 'center' },
          { label: '标签', prop: 'tags', width: 180, align: 'center' },
          { label: '题目数量', prop: 'totals', width: 180, align: 'center' },
          { label: '操作', width: 180, align: 'center', slotName: 'handle', width: 300, fixed: 'right'},
        ]"

        :pagination-props="{
          layout: 'sizes, prev, pager, next, jumper',
          total: subjectList.counts,
          currentPage: page,
          pageSize: pageSize,
          pageSizes: [10, 20, 30, 40],
        }"
        :pagination-events="{
          'current-change': onCurrentPageChange,
          'size-change': onPageSizeChange
        }"
      >
      <template v-slot:handle="scope" >
        <el-button 
          type="text" 
          @click="goDirectorys(scope.row)"
        >
          学科分类
        </el-button>
        <el-button 
          type="text" 
          @click="goTags(scope.row)"
        >
          学科标签
        </el-button>
        <el-button 
          type="text" 
          @click="onAddSubjectList(scope.row.id)"
        >
          修改
        </el-button>
        <el-button 
          type="text" 
          @click="onRemove(scope.row.id)"
        >
          删除
        </el-button>
      </template>
      </my-table>
    </el-card>
  </div>
</template>

<script>

let currentId = null
import { mapActions, mapState } from 'vuex'
import * as subjectApi from '../../api/hmmm/subjects'
export default {
  name: 'SubjectList',
  data () {
    return {
      loading: false,
      form: {
        subjectName: '',
      },
      subjectForm: {
        subjectName: '',
        isFrontDisplay: true,
      },
      page: 1,
      pageSize: 10,
      addVisible: false,
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapState('subject', ['subjectList', 'subjectDetail']),
  },
  methods: {
    ...mapActions('subject', ['updateSubjectList', 'updateSubjectDetail']),
    // 点击下一页
    onCurrentPageChange(val) {
      this.page = val
      this.updateSubjectList({
        page: this.page,
        pagesize: this.pageSize
      })
    },
    onPageSizeChange(val) {
      this.page = 1
      this.pageSize = val
      this.updateSubjectList({
      page: this.page,
      pagesize: this.pageSize
      })

    },
    // 清除按钮
    onRemoveAll() {
      this.form = {
        directoryName: '',
        state: ''
      }
    },
    // 搜索按钮
    async onSearchSubjectList() {
      this.loading = true
      await this.updateSubjectList({
        ...this.form,
        page: this.page,
        pagesize: this.pageSize
      })
      this.loading = false
    },
    // 删除按钮
    onRemove(id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.loading = true
        await subjectApi.removeSubject({id})
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.loading = false
        this.updateSubjectList({ page: this.page, pagesize: this.pageSize })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 点击新增/修改按钮
    async onAddSubjectList(id) {
      this.addVisible = true
      if(!isNaN(id)) {
        currentId = id
        await this.updateSubjectDetail({id})
        this.subjectForm.subjectName = this.subjectDetail.data.subjectName
        this.subjectForm.isFrontDisplay = this.subjectDetail.data.isFrontDisplay
      }else{
        this.subjectForm.subjectName = ''
        this.subjectForm.isFrontDisplay = true
        currentId = null
      }
    },
    // 点击确定新增/修改
    async onAddSubjectListOk(id) {
      this.loading = true
      if(currentId) {
        await subjectApi.updateSubject({
          ...this.subjectForm,
          id: currentId
      })
        .then(res => this.$message.success('操作成功'))
        .catch(err => this.$message.error('操作失败'))
      }else{
        await subjectApi.addSubject({
          ...this.subjectForm,
          id: currentId
      })
        .then(res => this.$message.success('操作成功'))
        .catch(err => this.$message.error('操作失败'))
      }
      this.loading = false
      this.addVisible = false
      this.updateSubjectList({ page: this.page, pagesize: this.pageSize })
    },
    // 去目录页面
    goDirectorys(scope) {
      this.$router.push({
        name: 'subjects-directorys',
        query: {
          id: scope.twoLevelDirectory
        }
      })
    },
    // 去标签页面
    goTags(scope) {
      this.$router.push({
        name: 'subjects-tags',
        query: {
          id: scope.twoLevelDirectory
        }
      })
    }

  },
  async created() {
    this.loading = true
    await this.updateSubjectList({ page: this.page, pagesize: this.pageSize })
    this.loading = false
  }
}
</script>

<style lang="scss" scoped>

.dashboard-container {
  padding: 16px;

  .remove-btn {
    margin-left: 18px;
  }

  .el-icon-film {
    color: #0000f5;
  }

  .addUser {
    float: right;
    background-color: #7ebf50;
    border: none;
  }

  ::v-deep .ql-editor {
    height: 300px;
  }
  ::v-deep img {
    width: 100%;
  }

  .el-icon-view {
    margin: 0 5px;
  }
  .articleInfo{
    padding: 0 16px;
  }
  .hint {
    margin-bottom: 10px;
  }
  .articleContent {
    border-top: 1px dashed #ccc;
    margin-top: 5px;
    background-color: #f5f5f5;
    padding: 0 16px;
  }
}
</style>
